package com.splic;

import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import com.splic.common.gen.DistributedRidesLock;
import com.splic.framework.jwt.filter.JwtFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * 启动程序
 * 
 * @author ruoyi
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@MapperScan("com.splic.*.mapper")
@EnableScheduling
@EnableCaching
@EnableMethodCache(basePackages = "com.splic.product.service")
@EnableAsync
@EnableCreateCacheAnnotation
public class RuoYiApplication
{
    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(RuoYiApplication.class, args);
        System.out.println("(♥◠‿◠)ﾉﾞ  乐汇产品管理平台启动成功   ლ(´ڡ`ლ)ﾞ  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }

    /**
     * 分布式锁
     */
    @Bean
    public DistributedRidesLock distributedRidesLock(StringRedisTemplate redisTemplate) {
        return new DistributedRidesLock(redisTemplate, "product");
    }

    /**
     * 注册ID生成器
     *
     * @return
     */
    /*@Bean
    public SnowflakeIdWorker snowflakeIdWorker(StringRedisTemplate redisTemplate) {
        return SnowflakeIdWorkerBuilder.builder(redisTemplate, "box-user-server:vm:code:count");
    }*/

    @Bean
    public RestTemplate restTemplate(){
        //解决把数据从 HttpResponse 转换成 Object 的时候，找不到合适的 HttpMessageConverter 来转换
//        restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

    @Bean
    public JwtFilter jwtFilter() {
        return new JwtFilter();
    }
}